package com.yanqu.road.dao.impl.activity.signindraw;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.signindraw.cross.CrossSignInDrawItemData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossSignInDrawItemDataDaoImpl extends TempDataObjectDao<CrossSignInDrawItemData> {

    public static String getCrossSignInDrawItemDataSql = "select * from `t_c_sign_in_draw_item` where `activityId`=?";
    public Map<Integer, CrossSignInDrawItemData> getCrossSignInDrawItemData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, CrossSignInDrawItemData> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossSignInDrawItemDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossSignInDrawItemData data = parse(rs);
                result.put(data.getRewardId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String updateCrossSignInDrawItemDataSql = "update `t_c_sign_in_draw_item` set getNum=?,uidList=? where activityId=? and rewardId=?";
    @Override
    protected boolean updateData(CrossSignInDrawItemData crossSignInDrawItemData) {
        crossSignInDrawItemData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossSignInDrawItemData.getGetNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSignInDrawItemData.getUidList())));
        para.put(i++, new DbParameter(Types.INTEGER, crossSignInDrawItemData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSignInDrawItemData.getRewardId()));
        if (getDb().executeNoneQuery(updateCrossSignInDrawItemDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossSignInDrawItemDataSql = "insert into `t_c_sign_in_draw_item`(activityId,rewardId,getNum,uidList) values (?,?,?,?)";
    @Override
    protected boolean addData(CrossSignInDrawItemData crossSignInDrawItemData) {
        crossSignInDrawItemData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossSignInDrawItemData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSignInDrawItemData.getRewardId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossSignInDrawItemData.getGetNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossSignInDrawItemData.getUidList())));
        if (getDb().executeNoneQuery(insertCrossSignInDrawItemDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossSignInDrawItemData parseData(ResultSet rs) throws SQLException {
        CrossSignInDrawItemData crossSignInDrawItemData = new CrossSignInDrawItemData();
        crossSignInDrawItemData.setActivityId(rs.getInt("activityId"));
        crossSignInDrawItemData.setRewardId(rs.getInt("rewardId"));
        crossSignInDrawItemData.setGetNum(rs.getInt("getNum"));
        crossSignInDrawItemData.setUidList(JSON.parseObject(rs.getString("uidList"), new TypeReference<List<String>>() {}));

        return crossSignInDrawItemData;
    }

}
